package com.cms.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.cms.entity.FriendLink;
import com.cms.mapper.FriendLinkMapper;
import com.cms.utils.DbUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 友情链接表 服务实现类
 * </p>
 *
 * @author songfayuan
 * @since 2021-11-12
 */
@Service
public class FriendLinkService extends ServiceImpl<FriendLinkMapper, FriendLink> {


    /**
     * 查找友情链接分页
     *
     * @param pageNumber
     *            页码
     * @param pageSize
     *            每页记录数
     * @param siteId
     *            站点ID
     * @return 友情链接分页
     */
    public Page<FriendLink> findPage(Integer pageNumber, Integer pageSize, Integer siteId){
        QueryWrapper<FriendLink> queryWrapper = new QueryWrapper();
        queryWrapper.eq("siteId",siteId);
        queryWrapper.orderByDesc("sort","createDate");
        return baseMapper.selectPage(new Page<>(pageNumber, pageSize),queryWrapper);
    }

    /**
     * 查找分组ID列表
     *
     * @param siteId
     *            站点ID
     * @return 分组ID列表
     */
    public List<String> findGids(Integer siteId){
        QueryWrapper<FriendLink> queryWrapper = new QueryWrapper();
        queryWrapper.select("gid");
        queryWrapper.eq("siteId",siteId);
        queryWrapper.groupBy("gid");
        queryWrapper.orderByAsc("gid");
        List<Object> objs = baseMapper.selectObjs(queryWrapper);
        return objs.stream().map(i->String.valueOf(i)).collect(Collectors.toList());
    }

    /**
     * 查找最大的分组ID
     *
     * @param siteId
     * 			站点ID
     * @return
     */
    public Integer findMaxGid(Integer siteId){
        String filterSql = " and siteId="+siteId;
        Object obj = SqlRunner.db().selectObj("select max(gid) from cms_friend_link where 1=1 "+filterSql);
        if(obj!=null){
            return Integer.valueOf(obj.toString());
        }
        return null;
    }

    /**
     * 查找友情链接
     *
     * @param orderBy
     *            排序
     * @param start
     *            起始位置
     * @param count
     *            数量
     * @param siteId
     *            站点ID
     * @return 友情链接
     */
    public List<FriendLink> findList(Integer gid, String orderBy, Integer start, Integer count, Integer siteId) {
        String filterSql = " and siteId="+siteId;
        if(gid != null){
            filterSql += " and gid="+gid;
        }
        String orderBySql = "";
        if(StringUtils.isBlank(orderBy)){
            orderBySql = DbUtils.getOrderBySql("sort desc,createDate desc");
        }else{
            orderBySql = DbUtils.getOrderBySql(orderBy+",sort desc");
        }
        String countSql=DbUtils.getCountSql(start, count);
        List<Map<String,Object>> maps = SqlRunner.db().selectList("select * from cms_friend_link where 1=1 "+filterSql+orderBySql+countSql);
        return maps.stream().map(i-> JSON.parseObject(JSON.toJSONString(i), FriendLink.class)).collect(Collectors.toList());
    }
}
